package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.maiji.cloud.entities.manager.Role;
import com.maiji.cloud.entities.manager.User;
import com.maiji.cloud.entities.manager.UserRole;
import com.maiji.cloud.mapper.UserRoleMapper;
import com.maiji.cloud.request.manager.UserRoleEditReqData;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.manager.UserResData;
import com.maiji.cloud.service.RoleService;
import com.maiji.cloud.service.UserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.BaseService;
import com.maiji.cloud.utils.UUID_MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    private RoleService roleService;

    @Override
    public Boolean addUserRoles(String roleId, List<String> userIds) {
        if (userIds == null ||  userIds.size() == 0) return false;
        List<UserRole> userRoleList = userIds.parallelStream().map(userId ->
                new UserRole().setUuId(UUID_MD5.getUUID()).setRoleId(roleId).setUserId(userId)).collect(Collectors.toList());;
        return insertBatch(userRoleList);
    }

    @Override
    public List<UserResData> findAllWithRoles(List<User> userList) {
        List<String> userIds = userList.parallelStream().map(User::getUuId).collect(Collectors.toList());
        List<UserRole> userRoleList = selectList(new EntityWrapper<UserRole>().in("user_id", userIds));
        List<String> roleIds = userRoleList.parallelStream().map(UserRole::getRoleId).collect(Collectors.toList());
        List<Role> roleList = roleService.selectBatchIds(roleIds);
        List<UserResData> userResDataList  = BaseService.dealWithOneMidMany(userList, "getUuId",
                UserResData.class, "setRoles", userRoleList, "getUserId",
                "getRoleId", roleList, "getUuId", Role.class);
        return userResDataList;
    }

    @Override
    public BaseResDto editUserRole(UserRoleEditReqData data) {
        String userId = data.getUserId();
        if (! delete(new EntityWrapper<UserRole>().eq("user_id", userId)))
            return new BaseResDto(Status.ERROR);
        List<String> roleIds = data.getRoleIds();
        if (roleIds.size() > 0) {
            List<UserRole> userRoleList = roleIds.parallelStream().map(roleId ->
                    new UserRole().setUuId(UUID_MD5.getUUID()).setUserId(userId).setRoleId(roleId)).collect(Collectors.toList());
            if (! insertBatch(userRoleList)) return new BaseResDto(Status.ERROR);
        }
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseResDto editRoleUser(String roleId, List<String> userIds, Boolean add) {
        if (add) {
            List<UserRole> userRoleList = userIds.parallelStream().map(userId ->
                    new UserRole().setUuId(UUID_MD5.getUUID()).setRoleId(roleId).setUserId(userId)).collect(Collectors.toList());
            if (! insertBatch(userRoleList)) return new BaseResDto(Status.ERROR);
        } else {
            Wrapper<UserRole> menuWrapper = new EntityWrapper<UserRole>()
                    .eq("role_id", roleId)
                    .in("user_id", userIds);
            if (! delete(menuWrapper)) return new BaseResDto(Status.ERROR);
        }
        return new BaseResDto(Status.SUCCESS);
    }

}
